﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MWC.DL
{
    public class NetcampSpeeches
    {
        /// <summary>
        /// Pobiera z bazy danych telefonu
        /// </summary>
        /// <returns></returns>
        public static List<NetcampWebProxy.Speech> GetNetcampSpeeches()
        {
            //todo z bazy
            throw new NotImplementedException();
        }

        /// <summary>
        /// sprawdza czy w proxy są wystąpienia od wskazanej daty
        /// </summary>
        /// <param name="fromDate">data od ktorej maja byc poszukiwane wystapienia</param>
        /// <returns></returns>
        public static NetcampWebProxy.Speech[] GetNewSpeeches(DateTime fromDate)
        {
            // HACK zdaje się że powinno być private i wywoływane jako "ASYNC" zawsze gdy GetNetcampSpeeches jest uruchamiane (?)

            NetcampWebProxy.WebProxy netcampProxy = new NetcampWebProxy.WebProxy();

            NetcampWebProxy.CallGetAllSpeechesFromDateResult results;

            bool SecoundTime = false;
        TwoTimes:
            try
            {
                results = netcampProxy.GetAllSpeechesFromDate(fromDate);

                if (results.resultSuccess)
                {
                    return results.speechesTable;
                }
            }
            catch (Exception e)
            {
                if (!SecoundTime)
                {
                    SecoundTime = true;
                    goto TwoTimes;
                }
            }

            return null;


        }

        /// <summary>
        /// Zapisuje wskazane wystapienia do bazy danych telefonu
        /// </summary>
        /// <param name="newSpeeches"></param>
        /// <returns></returns>
        public static bool SaveSpeechesToDatabase(NetcampWebProxy.Speech[] newSpeeches)
        {
            List<BL.SpeechMobile> speechList = new List<BL.SpeechMobile>();

            try
            {
                for (int i = 0; i < newSpeeches.Length; ++i)
                {
                    BL.SpeechMobile speech = new BL.SpeechMobile(newSpeeches[i]);
                    speechList.Add(speech);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("yyy probs w konwersji ex: "+e.ToString());
            }

            MWC.DAL.DataManager.SaveSpeakers(speechList);
            return true;
        }
    }
}
